#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

ll phi(ll n) {
  ll res = n;
  for (ll x = 2; x * x <= n; x++) {
    if (n % x == 0) {
      res = res / x * (x - 1);
      while (n % x == 0) n /= x;
    }
  }
  if (n > 1) res = res / n * (n - 1);
  return res;
}
ll mod(string& s, ll m) {
  ll res = 0;
  for (char c : s) {
    res = res * 10 + c - '0';
    res %= m;
  }
  return res;
}
void euler(string& s, ll m, ll& x, ll& y) {
  if (s.size() < 18) {
    x = stoll(s);
    y = x - 1;
  } else {
    x = mod(s, m);
    y = (x - 1 + m) % m;
    x += m, y += m;
  }
}
ll fastpow(ll b, ll p, ll m) {
  ll res = 1;
  while (p) {
    if (p & 1) res = res * b % m;
    b = b * b % m;
    p >>= 1;
  }
  return res;
}
void solve() {
  string a, b;
  ll c, d;
  cin >> a >> b >> c;
  d = phi(c);
  ll x = mod(a, c);
  ll y, z;
  euler(b, d, y, z);
  x = (fastpow(x, y, c) - fastpow(x, z, c) + c) % c;
  if (!x) x = c;
  cout << x;
  return;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) solve();
  return 0;
}